﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace iLangl.Model
{
	public class Project : DuplexObject, IHistory, IProject
	{
		
		public String Name { get; set; }
		public String Description { get; set; }
		public DateTime Deadline { get; set; }
		public String Status { get; set; }

		public WorkArea WorkArea { get; set; }
		public Product Product { get; set; }

		public String LicenseKey { get; set; }

		public Contact Owner { get; set; }
		public Contact TechnicalContact { get; set; }


		public ContentEntity[] сontents = null;
		public ContentEntity[] Contents 
		{
			get
			{
				return сontents;
			}
			set
			{
				сontents = value;
				updateContents();
			}
		}

		private Translation[] translation = null;
		public Translation[] Translations {
			get
			{
				return translation;
			}
			set
			{
				translation = value;
				updateTranslations();
			}
		}

		private void updateTranslations()
		{
			if(Translations == null)
				return;
			foreach(Translation trans in Translations)
			{
				trans.Project = this;
			}
		}

		private void updateContents()
		{
			if (Contents == null)
				return;

			foreach (ContentEntity content in Contents)
			{
				content.Project = this;
			}
			
		}
		public StateHistory[] History { get; set; }


		public override string ToString()
		{
			return string.Format("Project with Id=\"{0}\" Name=\"{1}\".", Id, Name);
		}

		#region Language Section

		public IList<Translation> GetTranslationsByLanguagePair(LanguagePair lpr)
		{
			IList<Translation> result = new List<Translation>();

			foreach (Translation translation in Translations)
			{
				if (translation.LanguagePair.IsEqual(lpr))
				{
					result.Add(translation);
				}
			}
			return result;
		}

		public IList<LanguagePair> GetProjectLanguagePairs()
		{
			Hashtable hresult = new Hashtable();
			foreach (Translation translation in Translations)
			{
				string key = string.Format("{0}_{1}", translation.LanguagePair.SourceLanguage.LanguageId, translation.LanguagePair.TargetLanguage.LanguageId);

				if (hresult[key] != null)
					continue; ;

				hresult[key] = translation.LanguagePair;
			}

			IList<LanguagePair> result = new List<LanguagePair>();

			foreach (LanguagePair lp in hresult.Values)
			{
				result.Add(lp);
			}

			return result;
		}

		private IList<Language> _languages = null;

		public IList<Language> GetProjectLanguages(bool refresh)
		{
			if ((!refresh) && (_languages != null))
				return _languages;



			List<Language> result = new List<Language>();
			Hashtable addedlanguages = new Hashtable();

			if (Contents != null)
				foreach (ContentEntity ce in Contents)
				{
					if (addedlanguages[ce.Language.LanguageId] == null)
					{
						result.Add(ce.Language);
						addedlanguages[ce.Language.LanguageId] = true;
					}
				}

			if (Translations != null)
				foreach (Translation trans in Translations)
				{
					if (addedlanguages[trans.LanguagePair.SourceLanguage.LanguageId] == null)
					{
						result.Add(trans.LanguagePair.SourceLanguage);
						addedlanguages[trans.LanguagePair.SourceLanguage.LanguageId] = true;
					}

					if (addedlanguages[trans.LanguagePair.TargetLanguage.LanguageId] == null)
					{
						result.Add(trans.LanguagePair.TargetLanguage);
						addedlanguages[trans.LanguagePair.TargetLanguage.LanguageId] = true;
					}
				}

			_languages = result;
			return result;
		}
		public void SetProjectLanguages(IList<Language> languages)
		{
			_languages = languages;
		}

		public Language FindLanguage(String languageId)
		{
			if (_languages == null)
				return null;

			foreach (Language lang in _languages)
			{
				if (string.Compare(languageId, lang.LanguageId, true) == 0)
				{
					return lang;
				}
			}

			return null;
		}
#endregion

		#region Content Section
		public ContentEntity FindContent(String contentId)
		{
			if (Contents == null)
				return null;

			foreach (ContentEntity ce in Contents)
			{
				if (string.Compare(ce.Id, contentId, true) == 0)
				{
					return ce;
				}
			}

			return null;
		}
		#endregion


		public void AddTranslation(Model.Translation translation)
		{
			if (Translations == null)
			{
				Translations = new Translation[] { translation };
			}
			else
			{
				List<Translation> translations = new List<Translation>();
				translations.AddRange(Translations);
				translations.Add(translation);
				Translations = translations.ToArray();
			}
		}

		public void AddContent(ContentEntity content)
		{
			if (Contents == null)
			{
				Contents = new ContentEntity[] { content };
			}
			else
			{
				List<ContentEntity> contents = new List<ContentEntity>();
				contents.AddRange(Contents);
				contents.Add(content);
				Contents = contents.ToArray();
			}
		}
	}
}
